package com.yamidragut.simpleebooksreader;

import java.util.ArrayList;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dropbox.sync.android.DbxFileInfo;
/**
 * Custom list adapter in order to show a default image for the cover of the book, the title of the book
 * and the date of the last modification
 */

public class CustomListAdapter extends ArrayAdapter<DbxFileInfo> {
	
	private ArrayList<DbxFileInfo> ebookInfos;
	private int viewResourceId;
	
	private EbookListActivity currentActivity;
	
	public static final String LOG = "CustomListAdapter";

	public CustomListAdapter(EbookListActivity currentActivity, int viewResourceId, ArrayList<DbxFileInfo> ebookInfos) {
		super(currentActivity, viewResourceId, ebookInfos);
		this.viewResourceId = viewResourceId;
		this.ebookInfos = ebookInfos;
		this.currentActivity = currentActivity;
	}
	
	
	// Defines the view of each row of the list
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {			
		
		LinearLayout listRow;
		
		LayoutInflater li = (LayoutInflater) this.currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listRow =  (LinearLayout) li.inflate(viewResourceId, null);

		// Put the text of this row of the list (the title of the book)
		TextView itemText = (TextView) listRow.findViewById(R.id.title_view);	
		itemText.setText(Util.stripExtension(Util.EPUB_FILE, ebookInfos.get(position).path.getName()));
		
		// Put the text of this row of the list (the title of the book)
		TextView dateText = (TextView) listRow.findViewById(R.id.date_view);	
		dateText.setText( (DateFormat.getMediumDateFormat(currentActivity).format(ebookInfos.get(position).modifiedTime)).toString());
	
		// Put the default image for the cover of the book
		ImageButton coverImage = (ImageButton) listRow.findViewById(R.id.cover_image);
		
		// Sets the image
		coverImage.setImageResource(R.drawable.ic_launcher);
		
		// Sets the color of the background in order to be transparente by default and blue when pressed
		coverImage.setBackgroundResource(R.drawable.list_icon_selectors);
		
		// Sets the listener for the double tap
		coverImage.setOnTouchListener(doubleTapListener(position));

		return listRow;
		
	}// end getView
	
	
	
	// Creates the double tap listener 
	private OnTouchListener doubleTapListener(final int position) {
		OnTouchListener listener = new OnTouchListener() {
		    private GestureDetector gestureDetector = new GestureDetector(CustomListAdapter.this.currentActivity, new GestureDetector.SimpleOnGestureListener() {
		        @Override
		        public boolean onDoubleTap(MotionEvent e) {		            
		        	currentActivity.onCoverDoubleTap(position);     	
		            return true;
		        }

		    });

		    @Override
		    public boolean onTouch(View v, MotionEvent event) {
//		        Log.d(LOG, "Raw event: " + event.getAction() + ", (" + event.getRawX() + ", " + event.getRawY() + ")");
		        gestureDetector.onTouchEvent(event);
		        return false;
		    }
		};
		
		return listener;
	}
	

}


